#pragma once
#include<iostream>
#include<string>
#include<sys/types.h>
#include<sys/time.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/stat.h>
#include<cstdio>
#include<cstring>
#include<fcntl.h>
#include<signal.h>
#include<pthread.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<vector>
#include<arpa/inet.h>
#include<sys/wait.h>
#include<functional>
#include <semaphore.h>
#include<jsoncpp/json/json.h>
#include<sys/select.h>
#include<signal.h>
#define INFO 1
#define ERROR -1
using namespace std;

class Request
{
public:
    int _a;
    int _b;
    char _op;
};
class Response
{
public:
    int result;
    int code=INFO;
};
class EndPoint
{
public:
    EndPoint(int sock,string message)
    :_sock(sock)
    ,_message(message)
    {}
      void Handler()
    {
        GetInfo();
        GetResult();
        SendInfo();
    }
    void GetInfo()
    {
        string message= _message;;
        int pos=-1;
        int n=message.size();
        for(int i=0;i<n;i++)
        {
            if(!(message[i]>='0'&&message[i]<='9'))
            {
                pos=i;
                break;
            }
        }
        if(pos==-1)
        {
            _response.code=ERROR;
            return;
        }
        char op=message[pos];
        int a=atoi(message.substr(0,pos).c_str());
        int b=atoi(message.substr(pos+1).c_str());
        _request._a=a;
        _request._b=b;
        _request._op=op;
    }
    void GetResult()
    {
	int code=_response.code;
        if(code==ERROR)
        {
            return;
        }
        char op=_request._op;
        int a=_request._a;
        int b=_request._b;
        int& result=_response.result;
        switch(op)
        {
            case '+':
            result=a+b;
            break;
            case '-':
            result=a-b;
            break;
            case '*':
            result=a*b;
            break;
            case '/':
            result=a/b;
            break;
        }
    }
    void SendInfo()
    {
        int code=_response.code;
        string send_info;
        if(code==ERROR)
        {
            send_info+="输入错误";
        }
        else
        {
            send_info+=to_string(_request._a);
            send_info+=_request._op;
            send_info+=to_string(_request._b);
            send_info+="=";
            send_info+=to_string(_response.result);
        }
        send(_sock,send_info.c_str(),send_info.size(),0);
    }
private:
    Request _request;
    Response _response;
    int _sock;
    string _message;
};
